<template>
  <div class="app-container">
    课程列表

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="courseQuery.name" placeholder="课程名称"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="courseQuery.status" clearable placeholder="课程状态">
          <el-option value="Normal" label="已发布"/>
          <el-option value="Draft" label="未发布"/>
        </el-select>
      </el-form-item>

    <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
    <el-button type="default" @click="resetData()">清空</el-button>
  </el-form>

    <!-- 表格 -->
  <el-table
    v-loading="listLoading"
    :data="list"
    element-loading-text="数据加载中"
    border
    fit
    highlight-current-row
    row-class-name="myClassList">

    <el-table-column
      label="序号"
      width="70"
      align="center">
      <template slot-scope="scope">
        {{ (page - 1) * limit + scope.$index + 1 }}
      </template>
    </el-table-column>

    <el-table-column label="课程信息" width="470" align="center">
      <template slot-scope="scope">
        <div class="info">
          <div class="pic">
            <img :src="scope.row.cover" alt="scope.row.title" width="150px">
          </div>
          <div class="title">
            <a href="">{{ scope.row.title }}</a>
            <p>{{ scope.row.lessonNum }}课时</p>
          </div>
        </div>

      </template>
    </el-table-column>

    <el-table-column label="创建时间" align="center">
      <template slot-scope="scope">
        {{ scope.row.gmtCreate.substr(0, 10) }}
      </template>
    </el-table-column>
    <el-table-column label="发布时间" align="center">
      <template slot-scope="scope">
        {{ scope.row.gmtModified.substr(0, 10) }}
      </template>
    </el-table-column>
    <el-table-column label="价格" width="100" align="center" >
      <template slot-scope="scope">
        {{ Number(scope.row.price) === 0 ? '免费' :
        '¥' + scope.row.price.toFixed(2) }}
      </template>
    </el-table-column>
    <el-table-column prop="buyCount" label="付费学员" width="100" align="center" >
      <template slot-scope="scope">
        {{ scope.row.buyCount }}人
      </template>
    </el-table-column>

    <!-- TODO -->
    <el-table-column label="操作" width="150" align="center">
      <template slot-scope="scope">
        <router-link :to="'/edu/course/info/'+scope.row.id">
          <el-button type="text" size="mini" icon="el-icon-edit">编辑课程信息</el-button>
        </router-link>
        <router-link :to="'/edu/course/chapter/'+scope.row.id">
          <el-button type="text" size="mini" icon="el-icon-edit">编辑课程大纲</el-button>
        </router-link>
        <el-button type="text" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>
import courseApi from '@/api/edu/course'

export default{
  name:'List',
  data() {
    return {
      listLoading:true,
      list: null,
      page: 1,
      limit: 8,
      total: 0,
      // 在template里面声明的属性就算没有存在再改属性里面,就会自动把没有的属性添加进此属性中
      // 当然, 自己添加属性准没错
      courseQuery: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(page = 1) {
      // 获取数据
      this.page = page
      courseApi.getCoursePageList(this.page, this.limit, this.courseQuery)
      .then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
      .catch(error => {
        console.log(error)
        this.listLoading = false
      })
    },
    resetData() {
      // 重新加载数据
      this.courseQuery = {}
      this.getList()
    },
    removeDataById(id){
      // 根据讲师ID来删除讲师
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        return courseApi.deleteCourseById(id)
        .then(response => {
          this.$message({
            type:'success',
            message:'删除成功'
          })
          this.getList()
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
    })
    }
  }
}
</script>

<style scoped>
</style>
